package org.dynobjects.core;

import java.util.HashMap;
import java.util.Map;

public class EPClassLoader extends ClassLoader {

	private static Map mapClasses = new HashMap();
	
	private static EPClassLoader classLoader;


	private EPClassLoader(ClassLoader parent) {
		super(parent);
	}
	
	private EPClassLoader(){
		super();
	}

	public static final EPClassLoader getInstance(ClassLoader parent){
		if(classLoader==null){
			classLoader = new EPClassLoader(parent);
		}
		return classLoader;
	}
	
	public static final EPClassLoader getInstance(){
		if(classLoader==null){
			classLoader = new EPClassLoader();
		}
		return classLoader;
	}
	public Class findClass(byte[] bs, String className) throws ClassNotFoundException {

		byte[] byteCode = (byte[]) mapClasses.get(className);
		Class returnValue = null;
		if (byteCode == null) {
			returnValue = defineClass(className, bs, 0, bs.length);
			mapClasses.put(className, bs);
		}
		super.resolveClass(returnValue);
		return returnValue;
	}

	public Class get(String name) throws ClassNotFoundException {
		byte[] byteCode = (byte[]) mapClasses.get(name);
		
		return defineClass(name, byteCode, 0, byteCode.length);
	}
	
	

}
